/**
* <b>Description:</b><br> 
* @author Reading.Reagan</a>
* @version 1.0
* @Note
* <b>ProjectName:</b> setting
* <br><b>PackageName:</b> com.golden.setting.service.impl
* <br><b>ClassName:</b> package-info
* <br><b>Date:</b> 2018年9月18日 下午12:24:18
*/
package com.golden.setting.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.golden.setting.dao.DictionaryMapper;
import com.golden.setting.domain.DictionaryDO;
import com.golden.setting.domain.ResultDO;
import com.golden.setting.entity.Dictionary;
import com.golden.setting.query.DictionaryExample;
import com.golden.setting.service.IDictionaryService;
import com.golden.setting.util.BeanUtilsExtends;
import com.golden.setting.util.Constant;
import com.golden.setting.util.StatusCode;


@RestController
@RequestMapping("/api")
public class DictionaryServiceImpl implements IDictionaryService {

	private static final Logger logger = LoggerFactory.getLogger(DictionaryServiceImpl.class);

	@Autowired
	private DictionaryMapper dictionaryMapper;

	@Transactional
	@PostMapping("/dic")
	public ResultDO<DictionaryDO> add(@RequestBody DictionaryDO domain) {

		Dictionary entity = new Dictionary();

		ResultDO<DictionaryDO> result = new ResultDO<DictionaryDO>();

		if (BeanUtilsExtends.copyProperties(entity, domain)) {
			logger.error(result.getErrorMsg());
			result.setSuccess(false);
			result.setErrorCode(StatusCode.COPY_ERROR.getCode());
			result.setErrorMsg(StatusCode.COPY_ERROR.getMsg());
			return result;
		}

		entity.setCreatetime(Calendar.getInstance().getTime());
		entity.setModifitime(entity.getCreatetime());
		entity.setIsDelete(Constant.VALID.getValue());

		int r = -1;
		
		try {
			r = dictionaryMapper.insertSelective(entity);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setErrorCode(StatusCode.ADD_ERROR.getCode());
			result.setErrorMsg(StatusCode.ADD_ERROR.getMsg());
			logger.error(e.getMessage());
			return result;
		}

		if (r < 1) {
			result.setSuccess(false);
			result.setErrorCode(StatusCode.ADD_ERROR.getCode());
			result.setErrorMsg(StatusCode.ADD_ERROR.getMsg());
			return result;
		}

		domain.setId(entity.getId());
		result.setData(domain);

		return result;
	}

	@Transactional
	@DeleteMapping("/dic/{id}")
	public ResultDO<DictionaryDO> delete(@PathVariable(value = "id") Integer id) {
		
		ResultDO<DictionaryDO> result = new ResultDO<DictionaryDO>();

		Dictionary entity = new Dictionary();

		entity.setId(id);
		entity.setIsDelete(Constant.DELETE.getValue());

		int r = -1;

		try {
			r = dictionaryMapper.updateByPrimaryKeySelective(entity);
		} catch (Exception e) {
			result.setSuccess(false);
			e.printStackTrace();
			result.setErrorCode(StatusCode.DELETE_ERROR.getCode());
			result.setErrorMsg(StatusCode.DELETE_ERROR.getMsg());
			logger.error(e.getMessage());
		}

		if (r < 1) {
			result.setSuccess(false);
			result.setErrorCode(StatusCode.DELETE_ERROR.getCode());
			result.setErrorMsg(StatusCode.DELETE_ERROR.getMsg());
			return result;
		}

		return result;
	}
	
	
	@GetMapping("/dic/{id}")
	public ResultDO<DictionaryDO> get(@PathVariable(value = "id") Integer id) {

		ResultDO<DictionaryDO> result = new ResultDO<DictionaryDO>();

		if (id < 1) {
			result.setSuccess(false);
			result.setErrorCode(StatusCode.GET_ERROR.getCode());
			result.setErrorMsg(StatusCode.GET_ERROR.getMsg());
			return result;
		}

		Dictionary entity = null;

		try {
			entity = dictionaryMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setErrorCode(StatusCode.GET_ERROR.getCode());
			result.setErrorMsg(StatusCode.GET_ERROR.getMsg());
			logger.error(e.getMessage());
			return result;
		}

		if (entity == null) {
			result.setSuccess(false);
			result.setErrorCode(StatusCode.GET_ERROR.getCode());
			result.setErrorMsg(StatusCode.GET_ERROR.getMsg());
			logger.error(StatusCode.GET_ERROR.getMsg() + " 获取的实体数据为空");
			return result;
		}
		
		
		result.setData(getDictionaryDO(entity));
		return result;
	}

	@PostMapping("/dics")
	public ResultDO<List<DictionaryDO>> query(@RequestBody DictionaryExample example) {
		
		ResultDO<List<DictionaryDO>> result = new ResultDO<List<DictionaryDO>>();

		
		// 是否进行分页
		if (example.isPage()) {
			example.setLimitByPage(example.getStart() + "," + example.getPageRows());
			long count = dictionaryMapper.countByExample(example);
			result.setCount(count);
		}
		List<Dictionary> list = null;
		try {
			list = dictionaryMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}

		result.setSuccess(true);
		result.setData(getDictionaryDOList(list));
		return result;
	}

	/**
	 * 对象拷贝单个实体到对象中
	 * 
	 * @param obj
	 * @return 返回DO对象
	 */
	public DictionaryDO getDictionaryDO(Dictionary entity) {
		if (entity == null)
			return null;
		DictionaryDO domain = new DictionaryDO();
		return BeanUtilsExtends.copyProperties(domain, entity) ? domain : null;
	}

	/**
	 * 对象拷贝多个实体到列表中
	 * 
	 * @param List
	 * @return List
	 */
	public List<DictionaryDO> getDictionaryDOList(List<Dictionary> list) {
		List<DictionaryDO> result = new ArrayList<DictionaryDO>();
		if (list != null && !list.isEmpty()) {
			for (Dictionary entity : list) {
				DictionaryDO domain = getDictionaryDO(entity);
				if (domain != null) {
					result.add(domain);
				}
			}
		}
		return result;
	}

}